/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zcm.modules.zhaocai.web.rest;

import com.alibaba.druid.support.json.JSONUtils;
import com.zcm.common.config.Constant;
import com.zcm.common.config.Global;
import com.zcm.common.mapper.JsonMapper;
import com.zcm.common.persistence.CrudDao;
import com.zcm.common.persistence.Page;
import com.zcm.common.utils.DateUtils;
import com.zcm.common.utils.StringUtils;
import com.zcm.common.utils.ZipUtils;
import com.zcm.common.web.BaseController;
import com.zcm.modules.cms.entity.Article;
import com.zcm.modules.cms.entity.ArticleData;
import com.zcm.modules.cms.entity.Category;
import com.zcm.modules.cms.entity.Site;
import com.zcm.modules.cms.service.ArticleDataService;
import com.zcm.modules.cms.service.ArticleService;
import com.zcm.modules.cms.utils.CmsUtils;
import com.zcm.modules.sys.utils.DictUtils;
import com.zcm.modules.sys.utils.UserUtils;
import com.zcm.modules.xgb.utils.XgbUtils;
import com.zcm.modules.zhaocai.entity.ZcmKanpanData;
import com.zcm.modules.zhaocai.service.ZcmKanpanDataService;
import com.zcm.common.web.BaseController;
import com.zcm.modules.zhaocai.entity.ZcmKanpanData;
import com.zcm.modules.zhaocai.service.ZcmKanpanDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 看盘数据Controller
 * @author www
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "/rest")
public class ZcmKanpanDataRestController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @Autowired
	private ZcmKanpanDataService zcmKanpanDataService;

    @Autowired
    private ArticleDataService articleDataService;

    @RequestMapping(value = "zcm/{type}",method = RequestMethod.POST)
	@ResponseBody
	public String getDdata(@PathVariable("type") String type,@RequestParam(value = "day",required = false,defaultValue = "")String day) {
        ZcmKanpanData zcmKanpanData;
        String toDay = DateUtils.getDate();
        if(StringUtils.isNotEmpty(day) && !toDay.equals(day)){
		    zcmKanpanData = zcmKanpanDataService.getDataByDay(Constant.mapTypes.get(type),day);
        }else{
            zcmKanpanData = zcmKanpanDataService.getLatestData(Constant.mapTypes.get(type));
        }
		if(zcmKanpanData!=null){
            return zcmKanpanData.getContent();
        }
        return "{\"zcm_error\":\"no data\"}";
	}

    @RequestMapping(value = "zcmall")
    @ResponseBody
    public String getDdataAll(@RequestParam(value = "day",required = false,defaultValue = "")String day) {
        String toDay = DateUtils.getDate();
        Map<String,String> map = new HashMap<String, String>();
        Boolean isLatest = StringUtils.isNotEmpty(day) && !toDay.equals(day);
        for (Map.Entry<String, String> entry : Constant.mapTypes.entrySet()) {
            String type = entry.getValue();
            ZcmKanpanData zcmKanpanData;
            if(isLatest){
                zcmKanpanData = zcmKanpanDataService.getDataByDay(type,day);
            }else{
                zcmKanpanData = zcmKanpanDataService.getLatestData(type);
            }
            if(zcmKanpanData==null){
                continue;
            }
            //map.put(type,ZipUtils.gzip(zcmKanpanData.getContent().replaceAll("\r\n","")));
            map.put(type,zcmKanpanData.getContent());
        }

        return JSONUtils.toJSONString(map).replaceAll("\r\n","");
    }


    @RequestMapping(value = "zcmArticleList")
    @ResponseBody
    public Map getDdata(@RequestParam(required=false, defaultValue="1") Integer pageNo,
                        @RequestParam(required=false, defaultValue="12") Integer pageSize) {
	    Map<String,Object> map = new HashMap<String, Object>();
        String categoryId = DictUtils.getDictValue("zcm_article","zcm_config_catalog","");
        Category category1 = CmsUtils.getCategory(categoryId);
        if(category1 == null){
            map.put("total",0);
            map.put("count",0);
            map.put("totalPages",0);
            map.put("data",new ArrayList());
            map.put("result",1);
            map.put("message","配置错误");
            return map;
        }
        String siteId = CmsUtils.getCategory(categoryId).getSite().getId();

        Page<Article> page = new Page<Article>(pageNo,pageSize);
        Category category = new Category(categoryId, new Site(siteId));
        category.setParentIds(categoryId);
        Article article = new Article(category);
        article.setDelFlag(Article.DEL_FLAG_NORMAL);
        page = articleService.findPage(page, article, false);
        map.put("total",page.getCount());
        map.put("totalPages",page.getTotalPage());
        map.put("count",page.getList().size());
        map.put("data",page.getList());
        map.put("result",0);
        map.put("message","success");
	    return map;
    }


    @RequestMapping(value = "zcmArticleInfo")
    @ResponseBody
    public Map getDdata(@RequestParam(required=false, defaultValue="1") String articleId) {
        Map<String,Object> map = new HashMap<String, Object>();
        ArticleData articleData = articleDataService.get(articleId);
        if(articleData == null){
            map.put("data",null);
            map.put("result",1);
            map.put("message","文章不存在");
            return map;
        }
        map.put("data",articleData);
        map.put("result",0);
        map.put("message","success");
        return map;
    }



//    @RequestMapping(value = "hisTask")
//    @ResponseBody
//    public Map startTask() {
//        Map<String,Object> map = new HashMap<String, Object>();
//        List<ZcmKanpanData> list = zcmKanpanDataService.findList(new ZcmKanpanData());
//        for (ZcmKanpanData d: list) {
//            XgbUtils.parseXgbData(d);
//        }
//        map.put("result",0);
//        map.put("message","success");
//        return map;
//
//    }
}